import numpy as np
import matplotlib.pyplot as plt
def model(x,theta):
    return x.dot(theta)

def cost(h,y):
    return 0.5*np.mean((h-y)**2)

def grad(x,y,iter0=5000,alpha=0.01):
    m,n=x.shape
    theta=np.zeros(n)
    J=np.zeros(iter0)
    for i in range(iter0):
        h=model(x,theta)
        J[i]=cost(h,y)
        dt=1/m*x.T.dot(h-y)
        theta-=alpha*dt
    return theta,J,h
if __name__ == '__main__':
    data=np.loadtxt('ex1data2.txt',delimiter=',')

    x=data[:,:-1]
    y=data[:,-1]

    #归一化
    min_x=np.min(x,axis=0)
    max_x=np.max(x,axis=0)
    x=(x-min_x)/(max_x-min_x)

    # #标准化
    # miu=np.mean(x,axis=0)
    # sigma=np.std(x,axis=0)
    # x=(x-miu)/sigma

    #拼接
    X=np.c_[np.ones(len(x)),x]

    theta, J, h=grad(X,y)
    plt.plot(J)
    plt.show()
    print(theta)


    plt.scatter(y,y)
    plt.scatter(y,h)
    plt.show()